package com.mystore.user.controller;


import com.mystore.CookieUtil;
import com.mystore.ServerResponse;
import com.mystore.user.pojo.User;
import com.mystore.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.mystore.user.service.IUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.ServerSocket;
import java.util.*;
import java.util.concurrent.TimeUnit;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liutl
 * @since 2019-05-06
 */
@RestController
@RequestMapping
public class UserController {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IUserService iUserService;




    @RequestMapping(value = "/login")
    public ServerResponse login(String username, String password, HttpSession session, HttpServletResponse httpServletResponse) {

        ServerResponse<User> response = iUserService.login(username, password);

        if (response.isSuccess()) {
            CookieUtil.writeLoginToken(httpServletResponse, session.getId());
            redisTemplate.opsForValue().set(session.getId(), JsonUtil.obj2String(response.getData()), 1800, TimeUnit.SECONDS);
        }

        return ServerResponse.createBySuccess("登录成功");
    }

    @RequestMapping(value = "register", method = RequestMethod.POST)
    public ServerResponse register(User user) {
        return iUserService.register(user);
    }


}

